//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"
#include "sagittarius/backend/llvm/support.h"
#include "sagittarius/io/io.h"
#include <stdio.h>
#include <iostream>
#include <sstream>

namespace Sagittarius {
    
    std::string sg_name_from_sym(symcode s, symtab* st) {
        char* str;
        str = sg_lex_buf2str(symtab_namek(st, s));
        std::string name(str);
        free(str);
        return name;
    }
    
    std::string pASTnodeRep(parserExp* e, symtab* st) {
        std::string r;
        switch(e->type) {
            case PAST_NODE_TYPE_ASSN:
                r = pASTnodeRep(e->a, st);
                r += " = ";
                r += pASTnodeRep(e->b, st);
                return r;
            case PAST_NODE_TYPE_CALL:
                r = sg_name_from_sym(e->lvalid, st);
                r += "(";
                if(e->b)
                    r += pASTnodeRep(e->b, st);
                r += ")";
                return r;
            case PAST_NODE_TYPE_FACCESS:
                r = sg_name_from_sym(e->lvalid, st);
                AN(e->a, "Missing feature");
                r += ".";
                r += pASTnodeRep(e->a, st);
                return r;
            case PAST_NODE_TYPE_SYMBOL:
                r = sg_name_from_sym(e->lvalid, st);
                return r;
            case PAST_NODE_TYPE_RVAL_LINK:
                // rval
                r = pASTnodeRep(e->a, st);
                // next
                if(e->b) {
                    r += ",";
                    r += pASTnodeRep(e->b, st);
                }
                return r;
            case PAST_NODE_TYPE_BBLOCK_LINK:
                AN(0, "Not supposed to get rep of basic block link");
                AN(e->a, "Missing link");
                r = pASTnodeRep(e->a, st);
                r += "\n";
                // next
                if(e->b) {
                    AT(e->b->type == PAST_NODE_TYPE_BBLOCK_LINK, "Expected basic body link");
                    r += pASTnodeRep(e->b, st);
                }
                return r;
            case PAST_NODE_TYPE_UINT: {
                std::stringstream ss;
                ss << e->v.u;
                r = ss.rdbuf()->str();
                return r;
            }
            default:
                AN(0, "Unknown expression");
                return "";
        }
    }
    
    std::string sg_name_val_rep(sg_name_val* m, symtab* st) {
        return "local " + sg_name_from_sym(m->s, st);
    }
    
}
